package com.jcloud.codetest1.component;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author
 * @date 2021-04-18 11:36
 */
@Configuration
public class TransportRecordExecutor {

    @Bean(name="recordExecutor")
    public Executor transportRecordExecutor( ) {
        return initExecutor(10,10,200,"transport-record-");
    }

    @Bean(name="failRecordExecutor")
    public Executor failRecordExecutor( ) {
        return initExecutor(10,10,200,"fail-record-");
    }

    private ThreadPoolTaskExecutor initExecutor(int coreSize, int maxSize, int queueSize, String prefix){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //配置核心线程数
        executor.setCorePoolSize(coreSize);
        //配置最大线程数
        executor.setMaxPoolSize(maxSize);
        //配置队列大小
        executor.setQueueCapacity(queueSize);
        //配置线程池中的线程的名称前缀
        executor.setThreadNamePrefix(prefix);
        //拒绝策略：将抛出 RejectedExecutionException.
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        //执行初始化
        executor.initialize();
        return executor;
    }
}
